import load from "./load.js";
export default {
    // force: 是否强制加载
    async loadLanguage(lang, force = false) {
        const { default: languagePackage } = await import(
            `https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/es/languages/${lang}.min.js`
        );
        hljs.registerLanguage(lang, languagePackage);
        console.info("hljs: 添加", lang, "语言成功");
    },
    async loadHljs() {
        if (!window.hljs) {
            await load(
                "https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/highlight.min.js"
            );
            await load(
                "https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/styles/default.min.css"
            );
        }
    },
    async highlight(code, lang = "javascript", options = {}) {
        await this.loadHljs();
        console.info("高亮", lang, "代码中");
        const isExist = hljs.listLanguages().some((i) => i === lang);
        if (!isExist) await this.loadLanguage(lang);
        return hljs.highlight(code, { language: lang, ignoreIllegals: true })
            .value;
    },
};
